package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Iterator;

public class UpdateCartQuantitiesServlet extends HttpServlet {

    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    private Cart cart;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        cart = (Cart)session.getAttribute("cart");

        Iterator<CartItem>cartItems = cart.getAllCartItems();
        while(cartItems.hasNext())
        {
            CartItem cartItem = cartItems.next();
            String itemId = cartItem.getItem().getItemId();
            try{
                int  quantity = Integer.parseInt(req.getParameter(itemId));
                cart.setQuantityByItemId(itemId,quantity);
                if(quantity<1)
                {
                    cartItems.remove();
                }

            }catch (Exception e)
            {
                session.setAttribute("messgae","The Quantities of Item must be Integer");
                req.getRequestDispatcher(ERROR).forward(req,resp);
            }
        }
        req.getRequestDispatcher(VIEW_CART).forward(req,resp);

    }
}
